package com.igoal.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.dto.GoalDTO;
import com.igoal.dto.UserDTO;
import com.igoal.dto.UserGoalEntryDTO;
import com.igoal.service.CommonService;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.SaveResult;
import com.igoal.service.UserManagerExt;
import com.igoal.web.model.BaseModel;
import com.igoal.web.model.GoalEntryModel;

@Controller
@RequestMapping("/entry/editEntry.html")
public class GoalEntryController extends BaseController {

	private final Log log = LogFactory.getLog(GoalEntryController.class);
	private GoalDTO goalDetail;
	private UserGoalEntryDTO entry;

	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	@Autowired
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEnryManager) {
		this.goalEntryManager = goalEnryManager;
	}

	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String showEditPage(ModelMap modelMap, HttpServletRequest request,
			@RequestParam("id") Long id, @RequestParam("action") String action) {
		UserDTO user = getUserSession();
		if ("add".equals(action)) {
			goalDetail = goalManager.getGoalByPrimaryKey(id);
			entry = null;
		} else {
			entry = goalEntryManager.getEntry(id, user);
			if (entry == null) {
				return "redirect:userDashboard.html";
			}
			goalDetail = goalManager.getGoalByPrimaryKey(entry.getGoalId());
		}

		if (goalDetail == null) {
			return "redirect:userDashboard.html";
		}

		GoalEntryModel model = new GoalEntryModel();
		initTopPage(model, request);
		initRightPage(model, request);
		initLeftPage(model, request);

		applyModel(modelMap, model);
		return getViewPrefix() + "editEntry";
	}

	@RequestMapping(method = RequestMethod.POST)
	protected String saveEntry(@RequestParam("goalId") Long goalId,
			@RequestParam("entryId") Long entryId, @RequestParam("title") String title,
			@RequestParam("body") String body, @RequestParam("imgURL") String imgURL) {
		UserGoalEntryDTO entry = new UserGoalEntryDTO();
		entry.setTitle(title);
		entry.setBody(body);
		entry.setImgURL(imgURL);

		int saveStatus = SaveResult.FAIL;
		if (goalId != 0l) {
			entry.setGoalId(goalId);
			saveStatus = goalEntryManager.createNewGoalEntry(entry, getUserSession());
		} else if (entryId != 0l) {
			entry.setId(entryId);
			saveStatus = goalEntryManager.saveEntry(entry);
		}

		if (saveStatus == SaveResult.SUCCESS) {
			log.debug("Successfully update user's entry");
		} else {
			log.debug("Fail to update user's entry");
		}

		return "redirect:/dashboard.html";
	}

	public void initTopPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initTopZone(user, model);
	}

	public void initRightPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initRightZone(user, model);
		initPeopleYouMayKnow(user, model);
	}

	public void initLeftPage(BaseModel baseModel, HttpServletRequest request) {
		GoalEntryModel model = (GoalEntryModel) baseModel;
		model.setFamousQuote(commonService.getRandomFamousQuote());
		model.setGoalDetail(goalDetail);
		model.setEntry(entry);
	}
}
